<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${mapperPackagePath}.${entityName?cap_first}Mapper">

    <!-- 定义resultMap -->
    <resultMap id="resultMap" type="${poPackagePath}.${entityName?cap_first}Po">
    <#list classFieldInfoList as classFieldInfo>
        <result property="${classFieldInfo.name}" column="${classFieldInfo.name?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case}"/>
    </#list>
    </resultMap>

    <insert id="insert" parameterType="${poPackagePath}.${entityName?cap_first}Po">
        INSERT INTO ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list classFieldInfoList as classFieldInfo>
            <if test="${classFieldInfo.name} != null">${classFieldInfo.name?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case},</if>
        </#list>
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list classFieldInfoList as classFieldInfo>
            <if test="${classFieldInfo.name} != null"><#noparse>#{</#noparse>${classFieldInfo.name}<#noparse>}</#noparse>,</if>
        </#list>
        </trim>
    </insert>

    <insert id="insertBatch" parameterType="java.util.List">
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list classFieldInfoList as classFieldInfo>
            ${classFieldInfo.name?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case},
        </#list>
        </trim>
        values
        <foreach collection="list" item="item" index="index" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list classFieldInfoList as classFieldInfo>
                <#noparse>#{</#noparse>item.${classFieldInfo.name}<#noparse>},</#noparse>
            </#list>
            </trim>
        </foreach>
    </insert>

    <update id="update" parameterType="${poPackagePath}.${entityName?cap_first}Po">
        UPDATE ${tableName}
        <set>
        <#list classFieldInfoList as classFieldInfo>
        <#if classFieldInfo.name != "id">
            <if test="${classFieldInfo.name} != null and ${classFieldInfo.name} != ''">${classFieldInfo.name?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case} = <#noparse>#{</#noparse>${classFieldInfo.name}<#noparse>},</#noparse></if>
        </#if>
        </#list>
        </set>
        WHERE id = <#noparse>#{</#noparse>id<#noparse>}</#noparse>
    </update>

    <select id="selectById" resultType="${poPackagePath}.${entityName?cap_first}Po">
        select
            <trim suffixOverrides=",">
            <#list classFieldInfoList as classFieldInfo>
                ${classFieldInfo.name?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case},
            </#list>
            </trim>
        from ${tableName}
        where id = <#noparse>#{</#noparse>id<#noparse>}</#noparse>
    </select>

    <select id="selectByIdList" resultType="${poPackagePath}.${entityName?cap_first}Po">
        select
            <trim suffixOverrides=",">
            <#list classFieldInfoList as classFieldInfo>
                ${classFieldInfo.name?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case},
            </#list>
            </trim>
        from ${tableName}
        where id in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            <#noparse>#{</#noparse>id<#noparse>}</#noparse>
        </foreach>
    </select>

    <select id="list" parameterType="${searchPoPackagePath}.${entityName?cap_first}SearchPo" resultMap="resultMap">
        SELECT
        <trim suffixOverrides=",">
            <#list classFieldInfoList as classFieldInfo>
            ${classFieldInfo.name?replace("([a-z])([A-Z]+)","$1_$2","r")?lower_case},
            </#list>
        </trim>
        FROM ${tableName}
        <where>
            <#list queryConditionList as queryCondition>
            <#if queryCondition.queryConditionType.code == "like">
            <if test="po.${queryCondition.name} != null and po.${queryCondition.name} != ''">
                AND ${queryCondition.nameInTable} LIKE concat('%', <#noparse>#{</#noparse>po.${queryCondition.name}<#noparse>}</#noparse>, '%')
            </if>
            <#elseif queryCondition.queryConditionType.code == "equals">
            <if test="po.${queryCondition.name} != null and po.${queryCondition.name} != ''">
                AND ${queryCondition.nameInTable} = <#noparse>#{</#noparse>po.${queryCondition.name}<#noparse>}</#noparse>
            </if>
            <#elseif queryCondition.queryConditionType.code == "greaterThan">
            <if test="po.${queryCondition.limitName} != null and po.${queryCondition.limitName} != ''">
                AND ${queryCondition.nameInTable} <![CDATA[ > ]]> <#noparse>#{</#noparse>po.${queryCondition.limitName}<#noparse>}</#noparse>
            </if>
            <#elseif queryCondition.queryConditionType.code == "greaterThanOrEquals">
            <if test="po.${queryCondition.limitName} != null and po.${queryCondition.limitName} != ''">
                AND ${queryCondition.nameInTable} <![CDATA[ >= ]]>  <#noparse>#{</#noparse>po.${queryCondition.limitName}<#noparse>}</#noparse>
            </if>
            <#elseif queryCondition.queryConditionType.code == "less">
            <if test="po.${queryCondition.limitName} != null and po.${queryCondition.limitName} != ''">
                AND ${queryCondition.nameInTable} <![CDATA[ < ]]> <#noparse>#{</#noparse>po.${queryCondition.limitName}<#noparse>}</#noparse>
            </if>
            <#elseif queryCondition.queryConditionType.code == "lessThanOrEquals">
            <if test="po.${queryCondition.limitName} != null and po.${queryCondition.limitName} != ''">
                AND ${queryCondition.nameInTable} <![CDATA[ <= ]]>  <#noparse>#{</#noparse>po.${queryCondition.limitName}<#noparse>}</#noparse>
            </if>
            </#if>
            </#list>
        </where>
        ORDER BY id ASC
    </select>

    <delete id="deleteById">
        delete from ${tableName} where id = <#noparse>#{</#noparse>id<#noparse>}</#noparse>
    </delete>

    <delete id="deleteByIdList">
        delete from ${tableName}
        <where>
            id in
            <foreach collection="list" separator="," item="id" open="(" close=")">
                <#noparse>#{</#noparse>id<#noparse>}</#noparse>
            </foreach>
        </where>
    </delete>
</mapper>